<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 15:57
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class YearModel
{
    const TABLE = 'year';
    /**
     * 查询某个时间所属的工程年度
     * @param $time
     * @return mixed
     */
    public function findYear($time){
        $result = DB::table(self::TABLE)->where('endTime','>',$time)->where('startTime','<',$time)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @return mixed
     */
    public function lists()
    {
        return DB::table(self::TABLE)->orderBy('startTime', 'desc')->get()->toArray();
    }

    /**
     * @param array $data
     * @return array
     */
    public function info($data = [])
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param $startTime
     * @return mixed
     */
    public function checkYear($startTime)
    {
        return DB::table(self::TABLE)->where('endTime', '>', $startTime)->count();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(Array $data)
    {
        $insertData = [
            'name' => $data['name'],
            'startTime' => $data['startTime'] . ' 00:00:00',
            'endTime' => $data['endTime'] . ' 23:59:59',
        ];
        return DB::table(self::TABLE)->insert($insertData);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param $data
     * @return mixed
     */
    public function delete($data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }
}